<?php

require_once (SY_DIR . '/helpers/HTMLElement.php');

class AmpFormHelper extends Helper
{

  public $dataProvider;

  public $afterLabel = ' ';

  public function checkboxes($name, $data = null, $checkedKeys = null, $attribs = null, $params = null)
  {
    $attribs = ParameterHolder::parseFrom($attribs);
    $params = ParameterHolder::parseFrom($params);
    
    $checkedKeys = (array) $checkedKeys;
    
    $e = new HTMLElement('p', $attribs->get('p'));
    
    foreach ( $data as $value => $label ) {
      $_attribs = new ParameterHolder($attribs->get('input'));
      $_attribs->set('name', $name . '[]');
      $_attribs->set('type', 'checkbox');
      $_attribs->set('value', $value);
      $_attribs->set('id', $name . '_' . $value);
      
      if (in_array($value, $checkedKeys)) {
        $_attribs->set('checked', 'true');
      }
      
      $option = new HTMLElement('input', $_attribs, $params);
      $option->append(' ');
      $option->append($label);
      $option->append($params->get('itemSeparator', '<br />'));
      $e->appendInner($option);
    }
    
    return $e;
  }

  public function fieldset($legend, $attribs = null)
  {
    $attribs = ParameterHolder::parseFrom($attribs);
    
    $e = new HTMLElement('fieldset', $attribs->get('fieldset'), null, new HTMLElement('legend', $attribs->get('legend'), null, $legend));
    
    $args = func_get_args();
    array_splice($args, 0, 2);
    
    foreach ( $args as $arg ) {
      $e->appendInner($arg);
    }
    
    return $e;
  }

  public function form($attribs = null)
  {
    $attribs = ParameterHolder::parseFrom($attribs);
    $attribs->setDefault('action', $this->response->makeUrl($this->request->get('route')));
    $attribs->setDefault('method', 'POST');
    
    $e = new HTMLElement('form', $attribs, null);
    
    $args = func_get_args();
    array_shift($args);
    
    foreach ( $args as $arg ) {
      $e->appendInner($arg);
    }
    
    return $e;
  }

  public function password($name, $label, $attribs = null)
  {
    $attribs = ParameterHolder::parseFrom($attribs);
    
    $attribs->set('label', ParameterHolder::parseFrom($attribs->get('label')));
    $attribs->get('label')->set('for', $name);
    
    $attribs->set('input', ParameterHolder::parseFrom($attribs->get('input')));
    $attribs->get('input')->copyAll(array (
      'type' => 'password', 'name' => $name 
    ));
    
    $e = new HTMLElement('p', $attribs->get('p'), null, new HTMLElement('label', $attribs->get('label'), null, $label), $this->afterLabel, new HTMLElement('input', $attribs->get('input')));
    return $e;
  }

  public function radios($name, $data = null, $selectedKey = null, $attribs = null, $params = null)
  {
    //
  }

  public function save($attribs = null)
  {
    return $this->submit(__('Save'), $attribs);
  }

  public function submit($label, $attribs = null)
  {
    $attribs = ParameterHolder::parseFrom($attribs);
    
    $attribs->set('input', ParameterHolder::parseFrom($attribs->get('input')));
    $attribs->get('input')->copyAll(array (
      'type' => 'submit', 'value' => $label 
    ));
    
    $e = new HTMLElement('p', $attribs->get('p'), null, new HTMLElement('input', $attribs->get('input')));
    return $e;
  }

  public function select($name, $label, $data = null, $value = null, $attribs = null, $params = null)
  {
    $value = $this->resolveValue($value, $params);
    
    $attribs = ParameterHolder::parseFrom($attribs);
    
    $attribs->set('label', ParameterHolder::parseFrom($attribs->get('label')));
    $attribs->get('label')->set('for', $name);
    
    $attribs->setDefault('select', ParameterHolder::parseFrom($attribs->get('select')));
    $attribs->get('select')->copyAll(array (
      'name' => $name 
    ));
    
    $e = new HTMLElement('p', $attribs->get('p'), null, new HTMLElement('label', $attribs->get('label'), null, $label), $this->afterLabel, new HTMLElement('select', $attribs->get('select'), null, $this->Form->options($data, $value)));
    
    return $e;
  }

  public function text($name, $label, $value = null, $attribs = null, $params = null)
  {
    $value = $this->resolveValue($value, $params);
    
    $attribs = ParameterHolder::parseFrom($attribs);
    
    $attribs->set('label', ParameterHolder::parseFrom($attribs->get('label')));
    $attribs->get('label')->set('for', $name);
    
    $attribs->set('input', ParameterHolder::parseFrom($attribs->get('input')));
    $attribs->get('input')->copyAll(array (
      'type' => 'text', 'name' => $name, 
      'value' => $value 
    ));
    
    $e = new HTMLElement('p', $attribs->get('p'), null, new HTMLElement('label', $attribs->get('label'), null, $label), $this->afterLabel, new HTMLElement('input', $attribs->get('input')));
    return $e;
  }

  public function textarea($name, $label, $value = null, $attribs = null, $params = null)
  {
    $value = $this->resolveValue($value, $params);
    
    $attribs = ParameterHolder::parseFrom($attribs);
    
    $attribs->set('label', ParameterHolder::parseFrom($attribs->get('label')));
    $attribs->get('label')->set('for', $name);
    
    $attribs->setDefault('textarea', ParameterHolder::parseFrom($attribs->get('textarea')));
    $attribs->get('textarea')->copyAll(array (
      'name' => $name 
    ));
    
    $e = new HTMLElement('p', $attribs->get('p'), null, new HTMLElement('label', $attribs->get('label'), null, $label), $this->afterLabel, new HTMLElement('textarea', $attribs->get('textarea'), null, $value));
    return $e;
  }

  public function resolveValue($value, $params = null)
  {
    if (! empty($params) && ParameterHolder::parseFrom($params)->has('dataProvider')) {
      $dataProvider = ParameterHolder::parseFrom($params)->get('dataProvider');
    }
    else {
      $dataProvider = $this->dataProvider;
    }
    
    if (empty($dataProvider)) return $value;
    
    if (is_callable($dataProvider)) {
      $value = (array) $value;
      return call_user_func_array($dataProvider, $value);
    }
    
    if (is_array($dataProvider)) return $dataProvider[$value];
  }

}

?>